package com.trendit.print.sdk.example;

import com.alibaba.fastjson.JSON;
import com.trendit.print.sdk.DefaultApiClient;
import com.trendit.print.sdk.models.printer.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 打印机Example
 */
public class PrinterExample {

    private static String appId = "1663177933839273984";
    private static String appKey = "c0190e337aae4da69d737b7151295576";
    private static String endpoint = "http://opensvr-gateway-test.juheba.top:8888";

    private static String deviceNo = "680030000846";

    public static void main(String[] args) throws InterruptedException {
        PrinterExample printerExample = new PrinterExample();
//        printerExample.unbindPrinter();
//        printerExample.bindPrinter();
//        printerExample.doPlayVoice();
        for (int i = 0; i < 200; i++) {
            printerExample.doPrint();
            Thread.sleep(50);
        }
//        printerExample.getPrintResult();
//        printerExample.getPrinterStatus();
//        printerExample.getWaitingQueueSize();
//        printerExample.cleanWaitingQueue();
//        printerExample.getWaitingQueueSize();
//        printerExample.setVolume();
//        printerExample.setSpeed();
//        printerExample.setDensity();
    }

    /**
     * 绑定打印机
     */
    void bindPrinter() {
        AddPrinterRequest.Model model = new AddPrinterRequest.Model();
        model.setSn(deviceNo);
        model.setName(deviceNo);
        model.setKey("111111");
        model.setLang(10);

        List<AddPrinterRequest.Model> modelList = new ArrayList<>();
        modelList.add(model);
        AddPrinterRequest addPrinterRequest = new AddPrinterRequest(modelList);


        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        AddPrinterResponse addPrinterResponse = defaultApiClient.execute(addPrinterRequest);
        System.out.println(JSON.toJSONString(addPrinterResponse));
    }

    /**
     * 解绑打印机
     */
    void unbindPrinter() {
        List<String> deviceNos = new ArrayList<>();
        deviceNos.add(deviceNo);

        DeletePrinterRequest deletePrinterRequest = new DeletePrinterRequest(deviceNos);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        DeletePrinterResponse deletePrinterResponse = defaultApiClient.execute(deletePrinterRequest);
        System.out.println(JSON.toJSONString(deletePrinterResponse));
    }

    /**
     * 给设备推送播报请求
     */
    void doPlayVoice() {
        PayInVoiceRequest.Model model = new PayInVoiceRequest.Model();
        model.setOrderId(UUID.randomUUID().toString());
        model.setSn(deviceNo);
        model.setPayChannel("2");
        model.setPayAmount(80000);
        model.setVoicePlayInterval(4);
        model.setVoicePlayTimes(1);
        model.setExpiredSec(120);
        PayInVoiceRequest payInVoiceRequest = new PayInVoiceRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        PayInVoiceResponse payInVoiceResponse = defaultApiClient.execute(payInVoiceRequest);

        System.out.println(JSON.toJSONString(payInVoiceResponse));
    }

    /**
     * 给设备推送打印请求
     */
    void doPrint() {
        DoPrintRequest.Model model = new DoPrintRequest.Model();
        model.setOrderId(UUID.randomUUID().toString());
        model.setCopies(1);
        model.setContent("HELLO PRINT API");
        model.setSn(deviceNo);
        model.setVoice("5");
        DoPrintRequest doPrintRequest = new DoPrintRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        DoPrintResponse doPrintResponse = defaultApiClient.execute(doPrintRequest);

        System.out.println(JSON.toJSONString(doPrintResponse));
    }

    /**
     * 查打印请求处理进度
     */
    void getPrintResult() {
        GetPrintResultRequest.Model model = new GetPrintResultRequest.Model();
        model.setSn(deviceNo);
        model.setPrintId(1696826146817691648L);
        GetPrintResultRequest getPrintResultRequest = new GetPrintResultRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        GetPrintResultResponse getPrintResultResponse = defaultApiClient.execute(getPrintResultRequest);

        System.out.println(JSON.toJSONString(getPrintResultResponse));
    }

    /**
     * 查设备是否就绪
     */
    void getPrinterStatus() {
        GetPrinterStatusRequest.Model model = new GetPrinterStatusRequest.Model();
        model.setSn(deviceNo);
        GetPrinterStatusRequest getPrintResultRequest = new GetPrinterStatusRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        GetPrinterStatusResponse getPrintResultResponse = defaultApiClient.execute(getPrintResultRequest);

        System.out.println(JSON.toJSONString(getPrintResultResponse));
    }

    /**
     * 清除设备待处理的任务（还没有来得及打印的请求）
     */
    void cleanWaitingQueue() {

        CleanWaitingQueueRequest.Model model = new CleanWaitingQueueRequest.Model();
        model.setSn(deviceNo);
        CleanWaitingQueueRequest getPrintResultRequest = new CleanWaitingQueueRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        CleanWaitingQueueResponse cleanWaitingQueueResponse = defaultApiClient.execute(getPrintResultRequest);

        System.out.println(JSON.toJSONString(cleanWaitingQueueResponse));
    }

    /**
     * 查设备当前有多少任务待处理
     */
    void getWaitingQueueSize() {
        GetWaitingSizeRequest.Model model = new GetWaitingSizeRequest.Model();
        model.setSn(deviceNo);
        GetWaitingSizeRequest getWaitingSizeRequest = new GetWaitingSizeRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        GetWaitingSizeResponse getWaitingSizeResponse = defaultApiClient.execute(getWaitingSizeRequest);

        System.out.println(JSON.toJSONString(getWaitingSizeResponse));
    }

    /**
     * 调整设备音量
     */
    void setVolume() {

        SetPrinterVolumeRequest.Model model = new SetPrinterVolumeRequest.Model();
        model.setSn(deviceNo);
        model.setVolume(1);

        SetPrinterVolumeRequest setPrinterVolumeRequest = new SetPrinterVolumeRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        SetPrinterVolumeResponse setPrinterVolumeResponse = defaultApiClient.execute(setPrinterVolumeRequest);

        System.out.println(JSON.toJSONString(setPrinterVolumeResponse));
    }

    /**
     * 调整设备打印速度
     */
    void setSpeed() {
        SetPrinterSpeedRequest.Model model = new SetPrinterSpeedRequest.Model();
        model.setSn(deviceNo);
        model.setSpeed(3);

        SetPrinterSpeedRequest setPrinterSpeedRequest = new SetPrinterSpeedRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        SetPrinterSpeedResponse setPrinterSpeedResponse = defaultApiClient.execute(setPrinterSpeedRequest);

        System.out.println(JSON.toJSONString(setPrinterSpeedResponse));
    }

    /**
     * 调整设备打印浓度
     */
    void setDensity() {
        SetPrinterDensityRequest.Model model = new SetPrinterDensityRequest.Model();
        model.setSn(deviceNo);
        model.setDensity(5);

        SetPrinterDensityRequest setPrinterDensityRequest = new SetPrinterDensityRequest(model);

        DefaultApiClient defaultApiClient = new DefaultApiClient(appId, appKey, endpoint);
        SetPrinterDensityResponse setPrinterDensityResponse = defaultApiClient.execute(setPrinterDensityRequest);

        System.out.println(JSON.toJSONString(setPrinterDensityResponse));
    }

    /**
     * 提前将符合条件要求的图片写入打印机。
     * 设备内存空间有限，如果没有相应的业务场景。可不使用此接口
     */
    void setImage() {

    }


}
